package com.ailianlian.ablecisi.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.ailianlian.ablecisi.baseclass.BaseViewModel;
import com.ailianlian.ablecisi.pojo.entity.AiCharacter;
import com.ailianlian.ablecisi.pojo.entity.Article;
import com.ailianlian.ablecisi.pojo.entity.Topic;
import com.ailianlian.ablecisi.repository.HomeRepository;
import com.ailianlian.ablecisi.result.Result;

import java.util.ArrayList;
import java.util.List;

public class HomeViewModel extends BaseViewModel {

    private final HomeRepository repository;
    private final MutableLiveData<List<AiCharacter>> characters;
    private final MutableLiveData<Article> featuredContent;
    private final MutableLiveData<List<Topic>> topics;

    private List<AiCharacter> allAiCharacters;
    private String currentCategory;

    public HomeViewModel(@NonNull Application application) {
        super(application, new MutableLiveData<>());
        this.repository = new HomeRepository(application);
        this.characters = new MutableLiveData<>(new ArrayList<>());
        this.featuredContent = new MutableLiveData<>();
        this.topics = new MutableLiveData<>(new ArrayList<>());
        // 初始化时设置所有AI角色和当前分类
        this.allAiCharacters = new ArrayList<>();
        this.currentCategory = "全部"; // 默认显示全部分类
    }

    public void filterByCategory(String category) {
        this.currentCategory = category;
        
        if ("全部".equals(category)) {
            characters.setValue(allAiCharacters);
            return;
        }

        List<AiCharacter> filtered = new ArrayList<>();
        for (AiCharacter aiCharacter : allAiCharacters) {
            if (aiCharacter.getType().contains(category)) {
                filtered.add(aiCharacter);
            }
        }
        
        characters.setValue(filtered);
    }

    public void loadData() {
        // 获取后端数据并更新LiveData
        repository.loadHotCharacters(new HomeRepository.DataCallback<List<AiCharacter>>() {
            @Override
            public void onSuccess(List<AiCharacter> data) {
                characters.postValue(data);
                allAiCharacters = data; // 保存所有AI角色数据
            }

            @Override
            public void onError(String msg) {
                // 可通过另一个 LiveData 通知 UI 错误
                getResultMutableLiveData().postValue(Result.error(msg));
            }

            @Override
            public void onNetworkError() {
                // 可通过另一个 LiveData 通知 UI 网络错误
                getResultMutableLiveData().postValue(Result.error("网络错误，请稍后重试"));
            }
        });

        repository.loadFeaturedContent(new HomeRepository.DataCallback<Article>() {
            @Override
            public void onSuccess(Article data) {
                featuredContent.postValue(data);
            }

            @Override
            public void onError(String msg) {
                // 可通过另一个 LiveData 通知 UI 错误
                getResultMutableLiveData().postValue(Result.error(msg));
            }

            @Override
            public void onNetworkError() {
                // 可通过另一个 LiveData 通知 UI 网络错误
                getResultMutableLiveData().postValue(Result.error("网络错误，请稍后重试"));
            }
        });

        repository.loadTopics(new HomeRepository.DataCallback<List<Topic>>() {
            @Override
            public void onSuccess(List<Topic> data) {
                topics.postValue(data);
                getResultMutableLiveData().postValue(Result.success("数据加载成功", data));
            }

            @Override
            public void onError(String msg) {
                // 可通过另一个 LiveData 通知 UI 错误
                getResultMutableLiveData().postValue(Result.error(msg));
            }

            @Override
            public void onNetworkError() {
                // 可通过另一个 LiveData 通知 UI 网络错误
                getResultMutableLiveData().postValue(Result.error("网络错误，请稍后重试"));
            }
        });
    }

    public LiveData<List<AiCharacter>> getCharacters() {
        return characters;
    }

    public LiveData<Article> getFeaturedContent() {
        return featuredContent;
    }

    public LiveData<List<Topic>> getTopics() {
        return topics;
    }
} 